package com.yYclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yYclass.controller.request.LinkGroupAddRequest;
import com.yYclass.controller.request.LinkGroupUpdateRequest;
import com.yYclass.interceptor.LoginInterceptor;
import com.yYclass.model.LinkGroupDO;
import com.yYclass.mapper.LinkGroupMapper;
import com.yYclass.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yYclass.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 栗永松
 * @since 2024-09-14
 */
@Service
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements LinkGroupService {

    @Override
    public int add(LinkGroupAddRequest addRequest) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
//        long accountNo = 122234441;
        LinkGroupDO groupDO = new LinkGroupDO();
        groupDO.setTitle(addRequest.getTitle());
        groupDO.setAccountNo(accountNo);
        return this.save(groupDO) ? 1 : 0;
    }

    @Override
    public int del(Long groupId) {
        LinkGroupDO groupDO = new LinkGroupDO();
        boolean remove = this.remove(new QueryWrapper<>(groupDO).eq("id", groupId)
                .eq("account_no", LoginInterceptor.threadLocal.get().getAccountNo()));
        return remove ? 1 : 0;
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        LinkGroupDO linkGroupDO = this.getOne((new QueryWrapper<LinkGroupDO>().eq("id", groupId).eq("account_no", LoginInterceptor.threadLocal.get().getAccountNo())));
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        // mapStruct
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        List<LinkGroupDO> linkGroupDOList = this.list(new QueryWrapper<>(new LinkGroupDO()).eq("account_no", LoginInterceptor.threadLocal.get().getAccountNo()));
        List<LinkGroupVO> groupVOList = linkGroupDOList.stream().map(obj -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj, linkGroupVO);
            return linkGroupVO;

        }).collect(Collectors.toList());
        return groupVOList;
    }

    @Override
    public int updateById(LinkGroupUpdateRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setId(request.getId());
        linkGroupDO.setAccountNo(accountNo);
        this.update(linkGroupDO,new QueryWrapper<LinkGroupDO>().eq("id",linkGroupDO.getId()).eq("account_no",linkGroupDO.getAccountNo()));
        return 1;
    }

    @Override
    public LinkGroupDO detailByIdAndNo(Long groupId, Long accountNo) {
        return this.getOne(new QueryWrapper<LinkGroupDO>().eq("id",groupId).eq("account_no",accountNo));
    }
}
